<?php
//header('Access-Control-Allow-Origin: http://www.baidu.com'); //设置http://www.baidu.com允许跨域访问
//header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With'); //设置允许的跨域header
$CONFIG = json_decode(preg_replace("/\/\*[\s\S]+?\*\//", "", file_get_contents(HDPHP_ORG_PATH . "Ueditor/php/config.json")), true);
//更改上传目录
$CONFIG['imagePathFormat'] = C('EDITOR_SAVE_PATH');
$action = $_GET['action'];
switch ($action) {
    case 'config':
        $result = json_encode($CONFIG);
        break;
    /* 上传图片 */
    case 'uploadimage':
        /* 上传涂鸦 */
    case 'uploadscrawl':
        /* 上传视频 */
    case 'uploadvideo':
        /* 上传文件 */
    case 'uploadfile':
        $upload = new Upload($CONFIG['imagePathFormat']);
        if ($info = $upload->upload()) {
            //加水印
            if ($_GET['water'] == 1) {
                $image = new Image();
                $image->water($info[0]['path']);
            }
            $info = $info[0];
            $result = json_encode(array(
                "state" => "SUCCESS", //上传状态，上传成功时必须返回"SUCCESS"
                "url" => $info['url'], //返回的地址
                "title" => $info['filename'], //新文件名
                "original" => $info['name'], //原始文件名
                "type" => $info['ext'], //文件类型
                "size" => $info['size'], //文件大小
            ));
        } else {
            $result = json_encode(array(
                'state' => $upload->error
            ));
        }
        break;

    /* 列出图片 */
    case 'listimage':
        $result = include("action_list.php");
        break;
    /* 列出文件 */
    case 'listfile':
        $result = include("action_list.php");
        break;

    /* 抓取远程文件 */
    case 'catchimage':
        $result = include("action_crawler.php");
        break;

    default:
        $result = json_encode(array(
            'state' => '请求地址出错'
        ));
        break;
}

/* 输出结果 */
if (isset($_GET["callback"])) {
    if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
        echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
    } else {
        echo json_encode(array(
            'state' => 'callback参数不合法'
        ));
    }
} else {
    echo $result;
}
exit;